package com.szosen.jkzj.activity.mbgy;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.szosen.jkzj.R;

public class ShowDialog {
	public static final int TYPE_HYP = 11;
	public static final int TYPE_GLY = 22;

	public static void show(Context context, final EditText editText, int type) {

		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		LinearLayout tl = (LinearLayout) inflater.inflate(R.layout.yyqk, null);
		if (type == TYPE_GLY) {
			((TextView) tl.findViewById(R.id.yyqk_tv1)).setText("【胰岛素】种类：");
			((TextView) tl.findViewById(R.id.yyqk_tv2)).setText("用法：");
			((TextView) tl.findViewById(R.id.yyqk_tv3)).setText("用量：");
		}
		String[] et_strs = editText.getText().toString().split(";");
		System.out.println(et_strs + "长度：" + et_strs.length);
		final EditText DrugName1_et = (EditText) tl
				.findViewById(R.id.yyqk_DrugName1);
		final EditText Usage1_et = (EditText) tl.findViewById(R.id.yyqk_Usage1);
		final EditText Consumption1_et = (EditText) tl
				.findViewById(R.id.yyqk_Consumption1);
		final EditText DrugName2_et = (EditText) tl
				.findViewById(R.id.yyqk_DrugName2);
		final EditText Usage2_et = (EditText) tl.findViewById(R.id.yyqk_Usage2);
		final EditText Consumption2_et = (EditText) tl
				.findViewById(R.id.yyqk_Consumption2);
		final EditText DrugName3_et = (EditText) tl
				.findViewById(R.id.yyqk_DrugName3);
		final EditText Usage3_et = (EditText) tl.findViewById(R.id.yyqk_Usage3);
		final EditText Consumption3_et = (EditText) tl
				.findViewById(R.id.yyqk_Consumption3);
		final EditText DrugNameOther_et = (EditText) tl
				.findViewById(R.id.yyqk_DrugNameOther);
		final EditText UsageOther_et = (EditText) tl
				.findViewById(R.id.yyqk_UsageOther);
		final EditText ConsumptionOther_et = (EditText) tl
				.findViewById(R.id.yyqk_ConsumptionOther);
		if (et_strs.length >= 12) {
			DrugName1_et.setText(et_strs[0]);
			Usage1_et.setText(et_strs[1]);
			Consumption1_et.setText(et_strs[2]);
			DrugName2_et.setText(et_strs[3]);
			Usage2_et.setText(et_strs[4]);
			Consumption2_et.setText(et_strs[5]);
			DrugName3_et.setText(et_strs[6]);
			Usage3_et.setText(et_strs[7]);
			Consumption3_et.setText(et_strs[8]);
			DrugNameOther_et.setText(et_strs[9]);
			UsageOther_et.setText(et_strs[10]);
			ConsumptionOther_et.setText(et_strs[11]);
		}
		builder.setView(tl);
		builder.setTitle("填写您的血压");
		if (type == TYPE_GLY || type == TYPE_HYP) {
			builder.setTitle("用药情况");
		}
		builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				String DrugName1 = returnNull(DrugName1_et.getText().toString()
						.trim());
				String Usage1 = returnNull(Usage1_et.getText().toString()
						.trim());
				String Consumption1 = returnNull(Consumption1_et.getText()
						.toString().trim());
				String DrugName2 = returnNull(DrugName2_et.getText().toString()
						.trim());
				String Usage2 = returnNull(Usage2_et.getText().toString()
						.trim());
				String Consumption2 = returnNull(Consumption2_et.getText()
						.toString().trim());
				String DrugName3 = returnNull(DrugName3_et.getText().toString()
						.trim());
				String Usage3 = returnNull(Usage3_et.getText().toString()
						.trim());
				String Consumption3 = returnNull(Consumption3_et.getText()
						.toString().trim());
				String DrugNameOther = returnNull(DrugNameOther_et.getText()
						.toString().trim());
				String UsageOther = returnNull(UsageOther_et.getText()
						.toString().trim());
				String ConsumptionOther = returnNull(ConsumptionOther_et
						.getText().toString().trim());

				String result = DrugName1 + ";" + Usage1 + ";" + Consumption1
						+ ";" + DrugName2 + ";" + Usage2 + ";" + Consumption2
						+ ";" + DrugName3 + ";" + Usage3 + ";" + Consumption3
						+ ";" + DrugNameOther + ";" + UsageOther + ";"
						+ ConsumptionOther;
				boolean tag = false;
				for (String s : result.split(";")) {
					// 如果有一个数据,就显示，否则只显示无
					if (!"无".equals(s)) {
						tag = true;
						break;
					}
				}
				if (tag) {
					editText.setText(result);
				} else {
					editText.setText("无");
				}
			}
		});
		builder.setNegativeButton("取消", null);
		builder.create().show();
	}

	private static String returnNull(String s) {
		if (TextUtils.isEmpty(s)) {
			return "无";
		} else {
			return s;
		}
	}
}
